package com.k2data.k2app.mapper;

import com.k2data.k2app.domain.DeviceCond;
import com.k2data.k2app.domain.po.Device;
import com.k2data.k2app.domain.po.DictPO;
import com.k2data.k2app.mybatis.mapper.K2Mapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author lidong9144@163.com 17-3-15.
 */
@Mapper
@Repository
public interface DeviceMapper extends K2Mapper<Device> {

    Device selectById(@Param("id") Long id);

    Device selectDevice(DeviceCond cond);

    Device selectByDeviceNo(String deviceNo);

    List<Device> selectDevices(Device cond);

    List<Device> selectDeviceByCustomerId(DeviceCond cond);

    List<Device> selectDeviceTypeStatusCount(DeviceCond cond);

    List<DictPO> selectDeviceCondition(DeviceCond cond);

    @Insert("INSERT INTO device_status_log(device_id, old_status, status, create_by) VALUES (#{deviceId}, #{oldStatus}, #{status}, #{createBy})")
    int insertStatusLog(@Param("deviceId") Long deviceId,
                        @Param("oldStatus") String oldStatus,
                        @Param("status") String status,
                        @Param("createBy") String createBy);

    /**
     * 根据 id 列表取出存在的 id, 用作 {@code List<Long> ids} 多个 id 参数验证
     */
    List<Long> selectIdsByIds(@Param("ids") List<Long> ids);

    Long selectDeviceTypeId(@Param("deviceId") Long deviceId);

    Long selectParentId(@Param("deviceId") Long deviceId);

    @Update("UPDATE device SET status = #{status} WHERE device_no = #{deviceNo}")
    int updateStatus(@Param("status") String status, @Param("deviceNo") String deviceNo);

}
